package com.matt.service.impl;

import com.matt.commons.dto.sso.StaffDTO;
import com.matt.domain.AreaLogo;
import com.matt.foundation.query.ChildrenActiveQuery;
import com.matt.repository.AreaLogoRepository;
import com.matt.repository.base.BaseNativeSqlRepository;
import com.matt.service.AreaLogoService;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Service
public class AreaLogoServiceImpl extends BaseNativeSqlRepository implements AreaLogoService {
    @Autowired
    AreaLogoRepository  areaLogoRepository;
    @Override
    public Object addOrUpdate(String name, Integer type, StaffDTO baseInfo, String id, String logUrl) {
        AreaLogo areaLogo=new AreaLogo();
        if (id != null) {
            areaLogo = areaLogoRepository.findById(id).get();
        }
        areaLogo.setAreaName(name);
        areaLogo.setLogoUrl(logUrl);
        areaLogo.setDeleted(false);
        areaLogo.setType(type);
        areaLogoRepository.save(areaLogo);
        return null;
    }

    @Override
    public Page<AreaLogo> page(ChildrenActiveQuery query) {
        Specification<AreaLogo> specification = new Specification<AreaLogo>() {
            @Override
            public Predicate toPredicate(Root<AreaLogo> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();

                // 添加条件：deleted字段为false
                predicates.add(criteriaBuilder.equal(root.get("deleted"), false));

                // 如果有其他查询条件，可以继续添加到predicates列表中

                // 应用所有条件
                if (!predicates.isEmpty()) {
                    query.where(predicates.toArray(new Predicate[0]));
                }

                return query.getRestriction();
            }
        };

        // 确保使用正确的pageNum和pageSize
        int pageNum = query.getPageNum();
        int pageSize = query.getPageSize(); // 获取页面大小
        if (pageSize < 1) {
            throw new IllegalArgumentException("Page size must be at least 1.");
        }

        // 创建PageRequest对象
        Pageable pageable = PageRequest.of(pageNum, pageSize);

        // 执行分页查询
        Page<AreaLogo> areaLogoPage = areaLogoRepository.findAll(specification, pageable);

        // 返回结果
        return new PageImpl<>(areaLogoPage.getContent(), pageable, areaLogoPage.getTotalElements());
    }

    @Override
    public Object delete(String id) {
        areaLogoRepository.deleteById(id);
        return null;
    }
}
